﻿/*
 * ========================================================================
 * Copyright(c) 2007-2012 情留メ蚊子, All Rights Reserved.
 * Welcom use the LiteFetion.
 * See more information,Please goto http://www.94qing.com
 * ========================================================================
 * 
 * 作 者：情留メ蚊子
 * Q  Q: 540644769
 * 邮 箱：qlwz@qq.com
 * 网 址：http://www.94qing.com
 * ========================================================================
*/
using System;
using System.Net;
using LiteFetion.Model;
using LiteFetion.Utils;
using Newtonsoft.Json.Linq;

namespace LiteFetion
{
    /// <summary>
    /// 用户管理
    /// </summary>
    public class UserManager
    {
        public UserManager(FetionClient client)
        {
            FetionClient = client;
        }

        /// <summary>
        /// FetionClient
        /// </summary>
        public FetionClient FetionClient { get; private set; }

        /// <summary>
        /// User
        /// </summary>
        public User User { get { return FetionClient.User; } }

        /// <summary>
        /// 设置心情短语
        /// </summary>
        /// <param name="impresa">心情短语</param>
        /// <returns></returns>
        public ActionResult SetImpresa(string impresa)
        {
            string postData = string.Format("ssid={0}&Impresa={1}",
                this.User.SessionId, impresa);
            HttpHelper http = new HttpHelper();
            http.Url = Gobal.WEBIM_URL_SET_PERSONAL_INFO.FormatWith(this.FetionClient.RequestVersion++);
            http.Method = Method.POST;
            http.PostData = postData;
            http.Cookies = this.User.Cookies;
            http.Do();

            this.FetionClient.Debug(http);

            if (http.StatusCode != HttpStatusCode.OK)
            {
                return ActionResult.HTTP_FAILED;
            }
            JObject json = Util.GetObjectResult(http.Html);
            if (json == null)
            {
                return ActionResult.JSON_FAILED;
            }
            int status = json.Value<int>("rc");
            if (status != 200)
            {
                return ActionResult.REQUEST_FAILED;
            }
            this.User.Impresa = impresa;
            return ActionResult.SUCCESS;
        }

        /// <summary>
        /// 设置状态
        /// </summary>
        /// <param name="state">状态:ONLINE, AWAY, BUSY, OFFLINE(HIDDEN)</param>
        /// <param name="custom">对这个状态的自定义说明，比如当状态为AWAY时，状态的说明可以是 "我吃饭去啦~"</param>
        /// <returns></returns>
        public ActionResult SetPresence(BuddyState state, string custom)
        {
            if (state != BuddyState.ONLINE && state != BuddyState.BUSY && state != BuddyState.AWAY && state != BuddyState.OFFLINE)
            {
                return ActionResult.SUCCESS;
            }
            string postData = string.Format("ssid={0}&Presence={1}&Custom={2}",
                this.User.SessionId, (int)state, custom.IfEmpty(""));
            HttpHelper http = new HttpHelper();
            http.Url = Gobal.WEBIM_URL_SET_PRESENCE.FormatWith(this.FetionClient.RequestVersion++);
            http.Method = Method.POST;
            http.PostData = postData;
            http.Cookies = this.User.Cookies;
            http.Do();

            this.FetionClient.Debug(http);

            if (http.StatusCode != HttpStatusCode.OK)
            {
                return ActionResult.HTTP_FAILED;
            }
            JObject json = Util.GetObjectResult(http.Html);
            if (json == null)
            {
                return ActionResult.JSON_FAILED;
            }
            int status = json.Value<int>("rc");
            if (status != 200)
            {
                return ActionResult.REQUEST_FAILED;
            }
            this.User.BuddyState = state;
            return ActionResult.SUCCESS;
        }
    }
}
